package com.wyd.tool;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

public class TableNode {

	private String tableName;
	private String tableOrdinaryName;
	private String namespace;
	private String type;
	private Node node;

	public String getPackages() {
		return type;
	}

	public void setPackages(String packages) {
		this.type = packages;
	}

	private List<Node> nodes = new ArrayList<>();

	public String getNamespace() {
		return namespace;
	}

	public void setNamespace(String namespace) {
		this.namespace = namespace;
	}

	public String getTableOrdinaryName() {
		return tableOrdinaryName;
	}

	public void setTableOrdinaryName(String tableOrdinaryName) {
		this.tableOrdinaryName = tableOrdinaryName;
	}

	public String getTableName() {
		return tableName;
	}

	public Node getPrimaryKey() {
		return node;
	}

	public void setPrimaryKey(Node node) {
		this.node = node;
	}

	public void setTableName(String tableName) {
		String tName = "";
		if (!StringUtils.isBlank(tableName)) {
			String[] fields = tableName.split("_");
			for (int i = 0; i < fields.length; i++) {
				if (i == 0) {
					tName += fields[i];
				} else {
					tName += captureName(fields[i]);
				}
			}
		}
		this.tableName = tName;
	}

	public List<Node> getNodes() {
		return nodes;
	}

	public void setNodes(List<Node> nodes) {
		this.nodes = nodes;
	}

	public static class Node {
		private String fieldType;// java对应类型
		private String myBatisType;// mybatis文件对应类型
		private String fieldName;
		private String ordinaryName;
		private String remark;// 注释
		private String isPrimaryKey = "1";// 是否主键 0为主键

		public String getFieldType() {
			return fieldType;
		}

		public String getIsPrimaryKey() {
			return isPrimaryKey;
		}

		public void setIsPrimaryKey(String isPrimaryKey) {
			this.isPrimaryKey = isPrimaryKey;
		}

		public void setFieldType(String fieldType) {
			Type type = Type.valuesOf(fieldType);
			this.fieldType = type.javaType;
			this.myBatisType = type.jdbcType.toUpperCase();
		}

		public String getMyBatisType() {
			return myBatisType;
		}

		public void setMyBatisType(String myBatisType) {
			this.myBatisType = myBatisType;
		}

		public String getFieldName() {
			return fieldName;
		}

		public String getOrdinaryName() {
			return ordinaryName;
		}

		public void setOrdinaryName(String ordinaryName) {
			this.ordinaryName = ordinaryName;
		}

		public String getRemark() {
			return remark;
		}

		public void setRemark(String remark) {
			this.remark = remark;
		}

		public void setFieldName(String fieldName) {
			String names = "";
			if (!StringUtils.isBlank(fieldName)) {
				String[] fields = fieldName.split("_");
				for (int i = 0; i < fields.length; i++) {
					if (i == 0) {
						names += fields[i];
					} else {
						names += captureName(fields[i]);
					}
				}
			}
			this.fieldName = names;
		}

	}

	/**
	 * 首字母大写
	 * 
	 * @author xuxiaoke
	 * @date 2016年5月13日 下午1:30:43
	 */
	public static String captureName(String name) {
		char[] cs = name.toCharArray();
		cs[0] -= 32;
		return String.valueOf(cs);

	}

}
